package StudentControl.StudentManager;

import Domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherIn {
    private ArrayList<Student> studentArrayList;
//Constructor================================================================
    public TeacherIn() {

    }

    public TeacherIn(ArrayList<Student> studentArrayList) {
        this.studentArrayList = studentArrayList;
    }
//Getter and Setter==========================================================
    public ArrayList<Student> getStudentArrayList() {
        return studentArrayList;
    }

    public void setStudentArrayList(ArrayList<Student> studentArrayList) {
        this.studentArrayList = studentArrayList;
    }
//Method=====================================================================
    public void teacherLogin(ArrayList<Student> studentsList){
        Scanner sc = new Scanner(System.in);
        loop:while (true) {
            System.out.println("============欢迎来到黑马教师管理系统============");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.返回上一级");
            System.out.println("6.退出");
            System.out.println("请输入您的选择：");
            String choice = sc.nextLine();
            //确认情况
            switch (choice) {
                case "1"://添加学生
                    addStudent(studentsList);
                    break;
                case "2"://删除学生
                    deleteStudent(studentsList);
                    break;
                case "3"://修改学生
                    updataStudent(studentsList);
                    break;
                case "4"://查看学生
                    queryStudent(studentsList);
                    break;
                case "5":
                    System.out.println("返回登录界面");
                    break loop;
                case "6":
                    System.out.println("谢谢您的使用");
                    System.exit(0);
                default:
                    System.out.println("您输入的信息有误，请重新输入：");
            }
        }
    }
    //学生管理系统用方法=======================================================================
    //修改学生对象
    private static void updataStudent(ArrayList<Student> studentsList) {
        //1.若无学生信息则提示无信息，并返回菜单
        if (studentsList.size() == 0) {
            System.out.println("当前没有学生信息，此功能无法使用。");
            return;
        }
        //2.输入需要修改的学生id
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生的id：");

        //3.用getIndex方法确认此id是否存在
        String id = sc.next();
        int index = getIndex(studentsList, id);
        if (index == -1) {
            //3.1 不存在学生对象，提示不存在
            System.out.println("学生id不存在");
            return;
        }

        //4.接收新学生信息
        //4.1 接收新学生姓名
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        //4.2 接收新学生年龄
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        //4.3 接收新学生生日
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        //5.创建新学生对象，并修改对应index集合
        Student stu = new Student(name, id, age, birthday);
        studentsList.set(index, stu);

        //6. 打印提示语句
        System.out.println("修改成功");

    }

    //删除学生对象
    private static void deleteStudent(ArrayList<Student> studentsList) {
        //1.若无学生信息则提示无信息，并返回菜单
        if (studentsList.size() == 0) {
            System.out.println("当前没有学生信息，此功能无法使用。");
            return;
        }
        //2.输入需要删除的学生id
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生的id：");
        //3.用getIndex方法确认此id是否存在
        String id = sc.next();
        int index = getIndex(studentsList, id);
        if (index == -1) {
            //3.1 不存在学生对象，提示不存在
            System.out.println("学生id不存在");
            return;
        }
        //3.2 remove方法删除学生
        studentsList.remove(index);

        //4. 打印提示语句
        System.out.println("删除成功");

    }

    //查询学生方法
    private static void queryStudent(ArrayList<Student> studentsList) {
        //1.若无学生信息则提示无信息，并返回菜单
        if (studentsList.size() == 0) {
            System.out.println("当前没有学生信息，请输入后再查询。");
            return;
        }
        //2.展示学生信息
        System.out.println("姓名   \t学号   \t年龄\t     生日");
        for (int i = 0; i < studentsList.size(); i++) {
            Student stu = studentsList.get(i);
            System.out.println(stu.getName()
                    + "\t" + stu.getId()
                    + "\t" + stu.getAge()
                    + "\t" + stu.getBirthday());
        }
    }

    //添加学生方法
    private static void addStudent(ArrayList<Student> studentsList) {
        //1.创建Scanner接收学生信息
        Scanner sc = new Scanner(System.in);
        //1.1接收学生id
        System.out.println("请输入学生id：");
        String id;
        while (true) {
            id = sc.next();
            int index = getIndex(studentsList, id);
            if (index == -1) {
                break;
            } else {
                System.out.println("该学生id已存在，请重新输入：");
            }
        }
        //1.2接收学生姓名
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        //1.3接收学生年龄
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        //1.4接收学生生日
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        //2.创建Student对象，存入接收信息
        Student stu = new Student(name, id, age, birthday);

        //3.将学生对象放入studentsList集合
        studentsList.add(stu);

        //4.提示添加成功
        System.out.println("学生信息添加成功");
    }

    //其他功能----------------------------
    //获取学生index，比对输入id是否已存在集合种
    public static int getIndex(ArrayList<Student> studentsList, String id) {
        int index = -1;
        for (int i = 0; i < studentsList.size(); i++) {
            Student stu = studentsList.get(i);
            if (id.equals(stu.getId())) {
                index = i;
                break;
            }
        }
        return index;
    }
}
